import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.InventionVO;

import views.invention.ActionsInformation;
import views.invention.AuctionClient;
import views.invention.AuctionFirm;
import views.invention.BasicInformation;
import views.invention.ClientInformation;
import views.invention.ExamplationInformation;
import views.invention.PractitionerInformation;
import views.invention.UserInformation;
import views.invention.ValueInformation;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private var _invention:InventionVO;

public function set invention(value:InventionVO):void
{
	_invention	=	value;
	refreshView();
}


public function get invention():InventionVO
{
	return _invention;
}

private function refreshView():void
{
	try
	{
		this.removeAllChildren();	
		addBasicInformation();
		addUserInformation();
		addActionsInformation();
		addAuctionInformation();
		addValueInformation();	
		addExamplationInformation();
		this.invalidateProperties();
	}
	catch(error:Error)
	{
		trace(error.getStackTrace());
		trace("error");
	}
}

private function addBasicInformation():void
{
	this.addChild(new BasicInformation());
}

private function addUserInformation():void
{		
	try
	{
		switch(model.role)
			{
				case 0:
					break;
				case 1:
					if(_invention.clientID == model.client.clientID)
					{
						this.addChild(new PractitionerInformation());
					}
					else
					{
						this.addChild(new UserInformation());	
					}
					break;
				case 2:
					if(_invention.inventorID == model.inventor.inventorID)
					{
						this.addChild(new PractitionerInformation());
					}
					else
					{
						this.addChild(new UserInformation());	
					}
					break;
				case 3:
					if(_invention.firmID == model.firm.firmID)
					{
						this.addChild(new ClientInformation());
					}
					else
					{
						this.addChild(new UserInformation());	
					}
					break;
				case 4:
					if(_invention.practitionerID == model.practitioner.practitionerID)
					{
						this.addChild(new ClientInformation());
					}
					else
					{
						this.addChild(new UserInformation());	
					}
					break;	
			}
	}
	catch(error:Error){}
}

private function addActionsInformation():void
{
	this.addChild(new ActionsInformation());
}

private function addAuctionInformation():void
{
	try
	{
		var auction:DisplayObject;
		if(  model.role == 3 && _invention.bBid == 1 && _invention.firmID == 0)
		{
			auction	=	new AuctionFirm();
			
		}	
		if(model.role == 1)
		{
			auction	=	new AuctionClient();
			
		}
		if(auction != null)
		{
			this.addChild(auction);
			model.inventionView	=	this.getChildIndex(auction);
		}
	}
	catch(error:Error)
	{
	}
}

private function addValueInformation():void
{
	try
	{
		if(_invention.clientID == model.client.clientID && model.role == 1)
		{
			var valueInformation:ValueInformation	=	new ValueInformation();
			this.addChild(valueInformation);
		}
	}
	catch(error:Error)
	{
	}
}

private function addExamplationInformation():void
{
	if(_invention.status >= 6)
	{
		this.addChild(new ExamplationInformation());
	}
}

override protected function commitProperties():void
{
	try
	{
		if(this.numChildren > model.inventionView && model.inventionView > 0)
		{
			this.selectedIndex	=	model.inventionView;
			
			this.tabBar.selectedIndex	=	model.inventionView;
			//trace(this.tabBar.selectedIndex+"   this.tabBar.selectedIndex");
			model.inventionView	=	0;
		}
	}
	catch(error:Error)
	{
		
	}
	super.commitProperties();
}